<template>
	<view class="parent">
		<view class="avatar-container">
			<image class="avatar" :src="user.headPicUrl"></image>
			<view class="tip">{{tip}}</view>
		</view>

		<view class="content">
			<view class="tag-container">
				<view class="label">会员特权</view>
				<view class="list">
					<view class="item" v-for="(item, index) in tags" :key="index">
						<image :src="item.pic"></image>
						<view class="title">{{item.title}}</view>
					</view>
				</view>
			</view>

			<view class="tag-container">
				<view class="label">会员开通</view>
				<view class="list">
					<view class="item-price" :class="{active: selectIndex == index}" v-for="(item, index) in list" :key="index" @click="onItem(item, index)">
						<view class="month" :class="{active: selectIndex == index}">{{item.month}}个月</view>
						<view class="price" :class="{active: selectIndex == index}">
							￥
							<view class="big" :class="{active: selectIndex == index}">{{(item.money/(item.month*30)).toFixed(2)}}</view>
							/天
						</view>
						<view class="money" :class="{active: selectIndex == index}">￥{{item.money}}</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="bottom" @click="showDialogPay">￥{{selectItem.money}}立即开通</view>

		<uni-popup type="bottom" ref="pay">
			<view class="popup">
				<view class="title">
					选择支付方式
				</view>
				<view class="type" v-for="(item, index) in payTypes" :key="index" @click="onItemPay(index)">
					<image :src="item.icon"></image>
					<view class="tip">{{item.title}}</view>
					<z-checkbox :checked="item.checked"></z-checkbox>
				</view>
				<view>
					<z-button text="确定支付" @click.native="requestPay"></z-button>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				tip: '您尚未开通VIP会员',
				tags: [{
						pic: this.getImageUrl('vip_icon1.png'),
						title: '免费畅聊'
					},
					{
						pic: this.getImageUrl('vip_icon2.png'),
						title: '会员专属色'
					},
					{
						pic: this.getImageUrl('vip_icon3.png'),
						title: '查看谁看到我'
					},
					{
						pic: this.getImageUrl('vip_icon4.png'),
						title: '专属标识'
					},
					{
						pic: this.getImageUrl('vip_icon5.png'),
						title: '专属气泡'
					},
					{
						pic: this.getImageUrl('vip_icon6.png'),
						title: '专属vip客服'
					},
				],
				list: [],
				selectIndex: 0,
				selectItem: {},
				payTypes: [{
						payType: 1,
						icon: this.getImageUrl('pay_alipay.png'),
						title: '支付宝支付',
						checked: false
					},
					{
						payType: 2,
						icon: this.getImageUrl('pay_wechat.png'),
						title: '微信支付',
						checked: false
					},
				],
				payVisiable: false,
				payType: '',
				user: {},
			}
		},
		onLoad() {
			this.requestVipList();
		},
		onShow() {
			this.requestUser();
		},
		methods: {
			onItem(item, index) {
				this.selectItem = item;
				this.selectIndex = index;
			},
			showDialogPay() {
				this.$refs.pay.open()
			},
			onItemPay(index) {
				this.payTypes.forEach(item => {
					item.checked = false
				});
				this.payTypes[index].checked = true;
			},

			async requestUser() {
				let res = await this.$common.get('/api/getLoginUser');
				if (200 == res.code) {
					this.user = res.data;
					if (0 == this.user.vip) {
						this.tip = '已开通VIP,' + this.user.vipTime + '到期';
						uni.setStorageSync('vip', true)
					} else {
						uni.setStorageSync('vip', false)
					}
				}
			},
			async requestVipList() {
				let res = await this.$common.get('/api/vipPackage/list');
				if (200 == res.code) {
					this.list = res.data;
					this.selectItem = this.list[0];
					this.first = false;
				} else {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				}
			},
			async requestPay() {
				this.payVisiable = false;
				let payType = '';
				this.payTypes.forEach(item => {
					if (item.checked) {
						payType = item.payType;
					}
				});
				if (!payType) {
					uni.showToast({
						title: '请选择支付类型',
						icon: 'none'
					})
					return
				}

				let url = '/api/aliPay/tradeWapPay';
				if (2 == payType) {
					url = '/api/wxPay/transactions';
				}
				const params = {
					userId: uni.getStorageSync('userId'),
					projectType: '2', //支付类型1 (钻石充值)  支付类型2  （会员充值）
					packageId: this.selectItem.id,
					// money:'0.3'
				}
				let res = await this.$common.post(url, params);
				if (200 == res.code) {
					this.$refs.pay.close()
					const webview = plus.webview.create('', 'custom-webview');
					webview.loadURL(res.data.payUrl, {
						'Referer': 'http://api.ptszyxx.com://'
					});
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.parent {
		display: flex;
		flex-direction: column;
		align-items: center;
		height: 100vh;
		padding: 20rpx;

		.label {
			font-size: 28rpx;
			color: #ab7aff;
			font-weight: bold;
			margin: 20rpx;
		}

		.avatar-container {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 20rpx;

			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
			}

			.tip {
				margin-top: 10rpx;
				padding: 10rpx 20rpx;
				background-color: #eaeaea;
				border-radius: 50rpx;
				font-size: 20rpx;
				color: #777;
			}
		}

		.content {
			display: flex;
			flex-direction: column;
			align-items: center;
			flex: 1;
			width: 100%;

			.tag-container {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-top: 30rpx;
				background-color: #fff;
				border-radius: 15rpx;
				padding: 20rpx;
				width: 100%;
				box-sizing: border-box;

				.list {
					display: flex;
					flex-direction: row;
					flex-wrap: wrap;
					width: 100%;

					.item {
						display: flex;
						flex-direction: column;
						align-items: center;
						width: 33.3%;
						margin: 15rpx 0;

						image {
							width: 80rpx;
							height: 80rpx;
							border-radius: 50%;
						}

						.title {
							font-size: 24rpx;
							color: #888;
							margin-top: 15rpx;
						}
					}

					.item-price {
						display: flex;
						flex-direction: column;
						width: 203rpx;
						margin: 10rpx;
						box-sizing: border-box;
						border: 1rpx solid #ab7aff;
						border-radius: 20rpx;
						padding: 20rpx;

						.month {
							font-size: 24rpx;
							color: #999;
						}

						.month.active {
							color: #fff;
						}

						.price {
							display: flex;
							flex-direction: row;
							align-items: flex-end;
							font-size: 24rpx;
							color: #ab7aff;
							margin: 30rpx 0;

							.big {
								font-size: 36rpx;
							}
						}

						.price.active {
							color: #fff;
						}

						.money {
							font-size: 28rpx;
							color: #555;
						}

						.money.active {
							color: #fff;
						}
					}

					.item-price.active {
						background-image: linear-gradient(to bottom, #9fe9ff, #ab7aff);
					}
				}
			}

		}

		.bottom {
			margin: 20rpx 50rpx;
			box-sizing: border-box;
			background-image: linear-gradient(to right, #9fe9ff, #ab7aff);
			border-radius: 40rpx;
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			color: white;
		}
	}

	.popup {
		display: flex;
		flex-direction: column;
		justify-content: center;
		background-color: white;
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;
		width: 100wh;
		height: 450rpx;
		padding: 30rpx;

		.title {
			font-size: 36rpx;
			color: #222;
			line-height: 60rpx;
			height: 60rpx;
			text-align: center;
		}

		.type {
			flex: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
			width: 100%;
			height: 100rpx;

			image {
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
				margin-right: 20rpx;
			}

			.tip {
				font-size: 28rpx;
				color: #555;
				flex: 1;
				text-align: left;
			}

		}
	}
</style>